package com.ptmall.app.net.base;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;


import com.facebook.stetho.common.LogUtil;
import com.ptmall.app.App;
import com.ptmall.app.R;
import com.ptmall.app.utils.NetWorkUtils;
import com.ptmall.app.view.MyProgressDialog;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

/**
 * Created by xuchaochao on 2017/8/18.
 * 网络请求结果回调
 */

public abstract class ApiNetResponse<T> {

    private final String TAG = "xucc";

    private Context mContext = App.getInstance().getApplicationContext();
    private String mLoadingTitle;

    private MyProgressDialog mLoadingDialog;

    /**
     * 使用该构造方法会 默认不显示加载框
     */
    public ApiNetResponse() {
    }

    /**
     * 使用该构造方法会 显示加载框
     * @param context
     */
    public ApiNetResponse(Context context, String loadingTitle) {
        if (null != context) {
            this.mContext = context;
            this.mLoadingTitle = loadingTitle;
            this.mLoadingDialog = new MyProgressDialog(context, loadingTitle);
        }
    }

    /**
     * 使用该构造方法会 显示加载框
     * @param context
     */
    public ApiNetResponse(Context context) {
        if (null != context) {
            this.mContext = context;
            this.mLoadingTitle = context.getString(R.string.hold_on);
            this.mLoadingDialog = new MyProgressDialog(context, mLoadingTitle);
        }

    }

    //Toast
    private Toast mToast;

    //必须要重写onSuccess处理
    public abstract void onSuccess(T data);

    /**
     * 接口开始请求时
     */
    public void onStart() {
        LogUtil.d(TAG, "netResponse onSubcribe()");
        //mLoadingDialog不为空时 才会显示
        showLoadingDialog();
    }


    public void processFaile(String info, T data, Throwable throwable) {
        dismissLoadingDialog();
        String errroInfo = info;
        if (null != throwable) {
            Log.d(TAG, Log.getStackTraceString(throwable));
        }
        if (null != throwable && TextUtils.isEmpty(info)) {
            if (throwable instanceof SocketTimeoutException) {//网络超时
                showToast(mContext.getString(R.string.error_net_timeout));
                errroInfo = mContext.getString(R.string.error_net_timeout);
            } else if (throwable instanceof ConnectException) {//网络异常
                showToast(mContext.getString(R.string.error_net_connect_ex));
                errroInfo = mContext.getString(R.string.error_net_connect_ex);
            } else {
                showToast(mContext.getString(R.string.error_unknow));
                errroInfo = mContext.getString(R.string.error_unknow);
            }
        }
        if (TextUtils.isEmpty(errroInfo)) {
            errroInfo = "请求失败！请稍候再试！";
        }

        onFaile(errroInfo, data, throwable);
    }

    /**
     * 接口访问结果失败时回调
     * @param info
     * @param data
     */
    public void onFaile(String info, T data, Throwable throwable) {
        LogUtil.d(TAG, "netResponse onFaile()");

        dismissLoadingDialog();

        if(info!=null&&!info.equals("处理失败")){
            showToast(info);
        }

//        String guidMod = "The user is not logged in";
//        if (!TextUtils.isEmpty(info) && info.equals(guidMod)) {
//            App.getInstance().setAccount(null);
//            Activity activity = App.getInstance().getNowActivity();
//            if (! (activity instanceof LoginActivity)) {
//                Intent intent = new Intent(activity, LoginActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//                activity.startActivity(intent);
//                activity.finish();
//            }
//        }


        showError(throwable);
    }

    /**
     * 当执行下载时，下载进度指示
     * @param process  值 0-100  代表 process%
     * @param status   0,1,2   1：代表下载成功，2 下载中， 0下载失败
     *
     * */
    public void onDownload(String process, int status) {
    }

    /**
     * 接口访问产生了异常
     * @param throwable
     */
    public void showError(Throwable throwable) {
        LogUtil.d(TAG, "netResponse onError()");
        if (null == throwable) {
            return;
        }

        dismissLoadingDialog();

        if (null != throwable) {
            Log.d(TAG, Log.getStackTraceString(throwable));
        }
        NetWorkUtils netWorkUtils = new NetWorkUtils();
        if (!netWorkUtils.isConnected()) {
            Toast.makeText(mContext,"网络错误", Toast.LENGTH_SHORT).show();
        }else {
        if (throwable instanceof SocketTimeoutException) {//网络超时
            showToast(mContext.getString(R.string.error_net_timeout));
        } else if (throwable instanceof ConnectException) {//网络异常
            showToast(mContext.getString(R.string.error_net_connect_ex));
        } else {

          //  showToast(mContext.getString(R.string.error_unknow));
        }}
    }

    /**
     * 处理完成
     */
    public void onComplete() {
        if (null != mLoadingDialog) {
            mLoadingDialog.dismiss();
        }
        LogUtil.d(TAG, "netResponse onComplete()");
    }

    public void showLoadingDialog() {
        if (null != mLoadingDialog) {
            mLoadingDialog.show();
        }
    }

    public void dismissLoadingDialog() {
        if (null != mLoadingDialog) {
            mLoadingDialog.dismiss();
        }
    }

    public void showToast(String info) {
        if (TextUtils.isEmpty(info)) {
            return;
        }

        if (null == mToast) {
            mToast = Toast.makeText(mContext, info, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(info);
        }
        mToast.show();
    }
}
